// pages/user/user-register/user-register.js
var httpUtil = require('../../../utils/httpUtil.js')
var emptyUtil = require('../../templates/empty/empty.js')
var uiUtil = require('../../../utils/uiUtil.js')
var util = require('../../../utils/util.js')
var that

const REMAIN_SEC = 60; //倒计时总时间60s


Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputUserName: null,
    inputPhontNo: null,
    inputSmsCode: null,
    inputAddress: null,
    inputCropName: null,
    inputCropArea: null,

    areaName: null,
    areaCode: null,

    sendSmsCodeStatus: 0, //0:默认,1:请稍候,2:倒计时
    remainSec: 60, //获取验证码倒计时
    timer: null,


    //地址选择器
    showLoadAddr: false, //true:地址列表正在加载中
    addrCurIndex: 0, //当前所在位置，0-3
    addrSelecteds: [], //已选择的区域
    addrDatas: [], //所有区域
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
  },
  onUnload: function() {
    var timer = this.data.timer;
    if (null != timer) {
      clearInterval(timer);
    }
  },
  onShareAppMessage: function (res) {
    return util.handleShareApp()
  },

  onUserNameInput(e) {
    var value = e.detail.value
    this.setData({
      inputUserName: value
    })
  },
  onPhoneNoInput(e) {
    var value = e.detail.value
    this.setData({
      inputPhontNo: value
    })
  },
  onSmsCodeInput(e) {
    var value = e.detail.value
    this.setData({
      inputSmsCode: value
    })
  },
  onAddressInput(e) {
    var value = e.detail.value
    this.setData({
      inputAddress: value
    })
  },
  onCropNameInput(e) {
    var value = e.detail.value
    this.setData({
      inputCropName: value
    })
  },
  onCropAreaInput(e) {
    var value = e.detail.value
    this.setData({
      inputCropArea: value
    })
  },
  /**
   * 获取手机验证码点击
   */
  requestGetSmsCode() {
    if (this.data.sendSmsCodeStatus != 0) {
      return;
    }
    var phoneNo = this.data.inputPhontNo
    if (!phoneNo) {
      uiUtil.showToast("请输入手机号码")
      return
    }

    //切换界面显示
    this.setData({
      sendSmsCodeStatus: 1,
    })

    //发送验证码
    httpUtil.PostRequest("send_sms_code", {
      phoneNo: phoneNo,
      codeType: 'userRegister',
    }, {
      success(res) {
        //切换界面显示
        that.setData({
          sendSmsCodeStatus: 2,
        })

        //开启倒计时
        var remain = that.data.remainSec
        var time = setInterval(function() {
          if (remain == 1) {
            clearInterval(time);
            that.setData({
              remainSec: REMAIN_SEC,
              sendSmsCodeStatus: 0,
            })
            return false
          }
          remain--;
          that.setData({
            remainSec: remain
          })
        }, 1000)
        that.setData({
          timer: time
        })
      },
      fail(code, res) {
        //切换界面显示
        that.setData({
          sendSmsCodeStatus: 0,
        })

        var text = emptyUtil.getErrText(code, res, "验证码发送失败，请稍候重试")
        uiUtil.showToast(text)
      },
      complete() {}
    })
  },

  requestRegister() {

  },

  /**
   * 激活
   */
  requestRegister() {
    var userName = this.data.inputUserName
    if (!userName) {
      uiUtil.showToast("请输入您的姓名")
      return
    }
    var phoneNo = this.data.inputPhontNo
    if (!phoneNo) {
      uiUtil.showToast("请输入手机号码")
      return
    }
    var smsCode = this.data.inputSmsCode
    if (!smsCode) {
      uiUtil.showToast("请输入验证码")
      return
    }
    var areaCode = this.data.areaCode
    if (!areaCode) {
      uiUtil.showToast("请选择所在地区")
      return
    }
    var areaName = that.data.areaName

    var address = this.data.inputAddress
    if (!address) {
      uiUtil.showToast("请输入详细地址")
      return
    }
    var cropName = this.data.inputCropName
    if (!cropName) {
      uiUtil.showToast("请输入种植作物")
      return
    }
    var cropArea = this.data.inputCropArea
    if (!cropArea) {
      uiUtil.showToast("请输入种植面积")
      return
    }

    uiUtil.showLoading("请稍候..")
    httpUtil.PostRequest("apply_join_vip", {
      userName: userName,
      phoneNo: phoneNo,
      smsCode: smsCode,
      areaCode: areaCode,
      areaName: areaName,
      address: address,
      cropName: cropName,
      cropArea: cropArea
    }, {
      success(res) {
        uiUtil.showToast("提交成功,请等待丰产管家与您取得联系")
        setTimeout(function() {
          wx.navigateBack({})
        }, 800)
      },
      fail(code, res) {
        var text = emptyUtil.getErrText(code, res, "信息提交失败,请稍候重试")
        uiUtil.showToast(text)
      },
      complete() {}
    })
  },




  /**
   * 地区选择点击
   */
  onAreaClick() {
    if (!this.data.addrDatas[0]) {
      this.addrChangeCurShow(0)
    }
    this.setData({
      showAddrView: true,
    })
  },

  /**
   * 已选择的地址标题点击事件
   */
  addrTitleClick(e) {
    if (this.data.showLoadAddr) { //正在加载中
      return;
    }
    var index = e.currentTarget.dataset.index
    this.addrChangeCurShow(index)
  },

  /**
   * 切换当前显示地区
   * index:第几个
   */
  addrChangeCurShow(index) {
    //检测是否需要网络加载
    try {
      if (that.data.addrDatas[index]) {
        if (0 == index) {
          this.setData({
            addrCurIndex: index,
          })
          return;
        } else {
          var parentId = that.data.addrDatas[index][0].parent_id
          var lastId = that.data.addrSelecteds[index - 1].id

          if (parentId == lastId) {
            this.setData({
              addrCurIndex: index,
            })
            return;
          }
        }
      }
    } catch (e) {}

    this.setData({
      showLoadAddr: true,
      addrCurIndex: index,
    })

    //加载地区列表
    var parentCode = "1"; //父及地区代码
    if (0 == index) {
      parentCode = "1";
    } else {
      parentCode = this.data.addrSelecteds[index - 1].id
    }

    httpUtil.PostRequest("select_area", {
      parentCode: parentCode
    }, {
      complete() {
        that.setData({
          showLoadAddr: false,
        })
      },
      success(res) {
        var addrDatas = that.data.addrDatas
        addrDatas[index] = res
        that.setData({
          addrDatas: addrDatas,
        })
      },
      fail(code, res) {}
    })
  },
  /**
   * 地址条目选择
   */
  addrItemClick(e) {
    var curTitleIndex = this.data.addrCurIndex //当前层级
    var addrSelecteds = this.data.addrSelecteds //

    var index = e.currentTarget.dataset.index
    var clickItem = this.data.addrDatas[curTitleIndex][index]

    addrSelecteds[curTitleIndex] = clickItem;

    if (3 == curTitleIndex) {
      //弹窗消失并返回
      this.setData({
        addrSelecteds: addrSelecteds,
        areaName: clickItem.allname,
        areaCode: clickItem.code,
        showAddrView: false,
      })
    } else {
      for (var i = curTitleIndex + 1; i < 4; i++) {
        addrSelecteds[i] = null
      }
      this.setData({
        addrSelecteds: addrSelecteds
      })
      this.addrChangeCurShow(curTitleIndex + 1)
    }
  },
  addrOutsideClick() {
    this.setData({
      showAddrView: false,
    })
  },
})
